<template>
  <a-modal width="78%" v-model="visible" title="参数配置" @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <j-form-container :disabled="formDisabled">
        <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
          <a-row>
            <a-col :span="24">
              <!-- <a-button
              class="editable-add-btn"
              @click="handleAdd"
              type="primary"
              :style="{ 'margin-bottom': '10px' }"
            >新增工程量</a-button>-->
              <a-table bordered :data-source="dataSource" :columns="columns" :pagination="false" :custom-row="customRow">
                <template slot="packageGridId" slot-scope="text, record">
                  <div>
                    <a-input v-model="record.gridName" v-if="packageGridIdList.length == 0"></a-input>
                    <a-select v-else placeholder="请选择" show-search optionFilterProp="children"
                      v-model="record.packageGridId"
                      @change="(e) => handleChange1(e, record.key, 'packageGridId', record, 1)">
                      <a-select-option v-for="mode in packageGridIdList" :key="mode.id" :value="mode.id">{{ mode.gridName
                      }}</a-select-option>
                    </a-select>
                  </div>
                </template>
                <!-- <template slot="calcCode" slot-scope="text, record">
                <div>
                  <a-select
                    placeholder="请选择"
                    @change="changeCalcCode(record)"
                    show-search
                    v-model="record.calcCode"
                    allowClear
                  >
                    <a-select-option
                      v-for="mode in calcTypeList"
                      :key="mode.id"
                      :value="mode.calcCode"
                    >{{ mode.calcName }}</a-select-option>
                  </a-select>
                </div>
              </template> -->
                <!-- <template slot="calcParam" slot-scope="text, record">
                <div>
                  <div v-if="record.calcCode!='gen_current_time'">
                    <a-input
                      @change="changeCalcParam(record)"
                      v-model="record.calcParam"
                      style="width:100px"
                    ></a-input>
                    <p v-if="record.validator" style="font-size:10px;color:red">计算参数必须不包含当前排序</p>
                  </div>

                  <span v-else>/</span>
                </div>
              </template> -->
                <template slot="defaultValue" slot-scope="text, record" style="display: flex;word-wrap:wrap;">
                  <div>
                    <div
                      v-if="(record.gridInputType == 'BYTE_STREAM_GROUP' || record.gridInputType == 'BYTE_STREAM') && record.calcCode !== 'gen_current_time'">
                      <a-input v-model="record.defaultValue"></a-input>
                    </div>
                    <div v-if="record.gridInputType == 'SELECT' && record.calcCode !== 'gen_current_time'">
                      <a-select placeholder="请选择" v-model="record.defaultValue" show-search optionFilterProp="children">
                        <a-select-option :value="item.packageGridValue" v-for="item in record.eigenValueList"
                          :key="item.id">{{ item.packageGridValue }}</a-select-option>
                      </a-select>
                      <!-- {{ record.eigenValueList }} -->
                      <!-- <a-input v-model="record.eigenValue"></a-input> -->
                    </div>
                    <!-- <a-input v-model="record.eigenValue" v-if="record.isEigen == 1 && record.length < 0"></a-input> -->
                    <div v-if="record.gridInputType == 'INPUT' && record.calcCode !== 'gen_current_time'">
                      <div style="display:flex;flex-wrap:wrap">
                        <span :key="index" v-for="(i, index) in record.length1">
                          <a-input :maxLength="2" style="width: 50px; margin-right: 10px" v-model="record.Values1[index]"
                            @change="
                              (val) => {
                                handleInput1(val, record)
                              }
                            "></a-input>
                        </span>
                        <div style="color: red" v-if="record.falseFlag1">请输入两位16进制数0-9,A-F</div>
                      </div>
                    </div>
                    <span v-if="!record.gridInputType || record.calcCode == 'gen_current_time'">/</span>
                    <!-- <template v-else>
                    {{ formatName(text, 'packageGridId') }}
                  </template>-->
                  </div>
                </template>
                <template slot="gridInputType" slot-scope="text, record">
                  <!-- <a-input disabled v-model="record.gridInputType"></a-input> -->
                  <j-dict-select-tag disabled v-model="record.gridInputType" dictCode="grid_input_type" />
                </template>
                <template slot="isEigen" slot-scope="text, record">
                  <div>
                    <a-select placeholder="请选择" v-model="record.isEigen"
                      @change="(e) => handleChange1(e, record.key, 'isEigen', record, 1)">
                      <a-select-option :value="1">是</a-select-option>
                      <a-select-option :value="0">否</a-select-option>
                    </a-select>
                    <!-- <a-input v-model="record.eigenValue" v-if="record.isEigen==1"></a-input> -->
                    <!-- <template v-else>
                    {{ formatName(text, 'packageGridId') }}
                  </template>-->
                  </div>
                </template>
                <template slot="eigenValue" slot-scope="text, record" min-width="180px">
                  <div>
                    <div v-if="
                      record.isEigen == 1 &&
                      (record.gridInputType == 'BYTE_STREAM_GROUP' || record.gridInputType == 'BYTE_STREAM')
                    ">
                      <a-input v-model="record.eigenValue"></a-input>
                    </div>
                    <div v-if="record.isEigen == 1 && record.gridInputType == 'SELECT'">
                      <a-select placeholder="请选择" v-model="record.eigenValue" show-search optionFilterProp="children">
                        <a-select-option :value="item.packageGridValue" v-for="item in record.eigenValueList"
                          :key="item.id">{{ item.packageGridValue }}</a-select-option>
                      </a-select>
                      <!-- <a-input v-model="record.eigenValue"></a-input> -->
                    </div>
                    <!-- <a-input v-model="record.eigenValue" v-if="record.isEigen == 1 && record.length < 0"></a-input> -->
                    <div v-if="record.isEigen == 1 && record.length > 0 && record.gridInputType == 'INPUT'">
                      <div>
                        <span :key="index" v-for="(i, index) in record.length">
                          <a-input :maxLength="2" style="width: 50px; margin-right: 10px" v-model="record.Values[index]"
                            @change="
                              (val) => {
                                handleInput(val, record)
                              }
                            "></a-input>
                        </span>
                        <div style="color: red" v-if="record.falseFlag">请输入两位16进制数0-9,A-F</div>
                      </div>
                    </div>
                    <span v-if="record.isEigen == 0">/</span>
                    <!-- <template v-else>
                    {{ formatName(text, 'packageGridId') }}
                  </template>-->
                  </div>
                </template>
                <!-- <template slot="packageInfoId" slot-scope="text, record">
                <div>
                  <div v-if="record.editable1">
                    <a-select placeholder="请选择" @change="(e) => handleChange1(e, record.key, 'packageInfoId')">
                      <a-select-option v-for="mode in PackageInfoList" :key="mode.id" :value="mode.id">
                        {{ mode.packageName }}
                      </a-select-option>
                    </a-select>
                  </div>

                  <template v-else>
                    {{ formatName(text, 'packageInfoId') }}
                  </template>
                </div>
              </template>-->
                <template slot="orderd" slot-scope="text, record">
                  <div>
                    <template>
                      <!-- {{ text }} -->
                      <!-- <a-input v-model="record.orderd" type="number" :min="1"></a-input> -->
                      <a-input-number id="inputNumber" v-model="record.orderd" :min="1" />
                    </template>
                  </div>
                </template>
                <template slot="operation" slot-scope="text, record">
                  <span style="margin-right: 10px" v-if="record.gridInputType == 'BYTE_STREAM_GROUP'">
                    <a @click="() => editChildren(record)">编辑</a>
                  </span>
                  <a-popconfirm style="margin-right: 5px"   overlayClassName="poperLayTable"  v-if="dataSource.length" title="确定删除该数据?"
                    @confirm="() => onDelete(record)">
                    <a href="javascript:;">删除</a>
                  </a-popconfirm>
                  <!-- <span v-if="record.editable1">
                  <a @click="() => save(record.key)" style="margin-right: 5px">保存</a>
                  <a-popconfirm title="确定取消吗?" @confirm="() => cancel(record.key)" style="margin-right: 5px">
                    <a>取消</a>
                  </a-popconfirm>
                </span>
                <span v-else style="margin-right: 5px">
                  <a :disabled="editingKey !== ''" @click="() => editK(record)">编辑</a>
                </span>-->

                  <div class="editable-row-operations"></div>
                </template>
              </a-table>
              <!-- <a-button
              class="editable-add-btn"
              @click="handleAdd"
              type="primary"
              :style="{ 'margin-bottom': '10px' }"
            >新增工程量</a-button> -->
            </a-col>
          </a-row>
        </a-form-model>
      </j-form-container>
      <!-- <add-children-modal ref="addChildrenModal" /> -->
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
// import addChildrenModal from './addChildrenModal.vue'
import Vue from 'vue'
const EditableCell = {
  template: `
      <div class="editable-cell">
        <div v-if="editable" class="editable-cell-input-wrapper">
          <a-input :value="value" @change="handleChange" @pressEnter="check" /><a-icon
            type="check"
            class="editable-cell-icon-check"
            @click="check"
          />
        </div>
        <div v-else class="editable-cell-text-wrapper">
          {{ value || ' ' }}
          <a-icon type="edit" class="editable-cell-icon" @click="edit" />
        </div>
      </div>
    `,
  props: {
    text: String,
  },

  data() {
    return {
      value: this.text,
      editable: false,
    }
  },
  methods: {
    handleChange(e) {
      const value = e.target.value
      this.value = value
    },
    check() {
      this.editable = false
      this.$emit('change', this.value)
    },
    edit() {
      this.editable = true
    },
  },
}
// const data = []
// for (let i = 0; i < 100; i++) {
//   data.push({
//     key: i.toString(),
//     name: `Edrward ${i}`,
//     packageInfoId: 32,
//     orderd: `London Park no. ${i}`,
//   })
// }
export default {
  name: 'NmcPackageInfoForm',
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  components: {
    EditableCell,
    // addChildrenModal,
  },
  data() {
    return {
      visible: false,
      calcTypeList: [
      ],
      targetObj: {},
      sourceObj: {},
      falseFlag: false,
      length: 2,
      model: {
        dataSource: [
          {
            key: '0',
            packageGridId: '',
            isEigen: 0,
            eigenValue: null,
            eigenValueList: [],
            Values: [],
            Values1: [],
            length: 1,
            falseFlag: false,
            falseFlag1: false,
            packageInfoId: this.packageInfoId,
            orderd: '1',
            editable1: true,
            calcCode: "",
            calcParam: "",
            validator: false,
          },
        ],
        Values: [],
      },
      packageInfoId: '',
      editingKey: '',
      cacheData: [],
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {},
      is1553B: false,
      url: {
        add: '/grid/nmcPackageInfo/add',
        addP: '/grid/nmcRelationPackageGrid/add',
        deleteP: '/grid/nmcRelationPackageGrid/delete',
        query: '/grid/nmcRelationPackageGrid/list',
        editP: '/grid/nmcRelationPackageGrid/edit',
        edit: '/grid/nmcRelationPackageGrid/edit',
        queryById: '/grid/nmcPackageInfo/queryById',
        PackageGridlist: '/grid/nmcPackageGrid/list',
        PackageInfoList: '/grid/nmcPackageInfo/list',
        nmcPackageGridValueList: '/grid/nmcPackageGridValue/list',
        calcTypeList: '/packageGridCalcType/nmcPackageGridCalcType/list'
      },
      dataSource: [
        {
          key: '0',
          packageGridId: '',
          packageInfoId: this.packageInfoId,
          orderd: '1',
          isEigen: 0,
          eigenValue: null,
          eigenValueList: [],
          Values: [],
          Values1: [],
          falseFlag: false,
          falseFlag1: false,
          length: 1,
          editable1: true,
          calcCode: "",
          calcParam: "",
          validator: false,
        },
      ],
      count: 0,
      packageGridIdList: [],
      ipagination: [
        {
          isorter: {
            order: 'asc',
            column: 'orderd',
          },
        },
      ],

      columns: [
        {
          title: '工程量',
          width: 180,
          dataIndex: 'packageGridId',
          scopedSlots: { customRender: 'packageGridId' },
        },
        // {
        //   title: '输入方式',
        //   dataIndex: 'gridInputType',
        //   scopedSlots: { customRender: 'gridInputType' },
        // },
        // {
        //   title: '是否特征值',
        //   dataIndex: 'isEigen',
        //   width: 110,
        //   scopedSlots: { customRender: 'isEigen' },
        // },
        // {
        //   title: '特征值',
        //   dataIndex: 'eigenValue',
        //   minWidth: 160,
        //   scopedSlots: { customRender: 'eigenValue' },
        // },
        // {
        //   title: '计算类型',
        //   dataIndex: 'calcCode',
        //   width: 160,
        //   scopedSlots: { customRender: 'calcCode' },
        // },
        // {
        //   title: '计算参数',
        //   dataIndex: 'calcParam',
        //   width: 140,
        //   scopedSlots: { customRender: 'calcParam' },
        // },
        {
          title: '默认值',
          dataIndex: 'defaultValue',
          width: 190,
          scopedSlots: { customRender: 'defaultValue' },
        },
        // {
        //   title: '组包排序',
        //   dataIndex: 'orderd',
        //   width: 100,
        //   // scopedSlots: { customRender: 'orderd' },
        //   customRender: (text, record, index) => {
        //     return index + 1
        //   }
        // },
        {
          title: '操作',
          dataIndex: 'operation',
          width: 120,
          scopedSlots: { customRender: 'operation' },
        },
      ],
      PackageInfoList: [],
      eigenValueList: [],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    this.cacheData = [...this.dataSource]
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.getCalcTypeList()
    // document.addEventListener('keyup', this.tabForward) //光标自动切换
  },
  methods: {
    showModal(id) {
      this.visible = true;
    },
    handleOk(e) {
      console.log(e);
      // this.visible = false;
      let data = []
      this.dataSource.forEach((i, index) => {

        if (i.gridInputType == 'INPUT' && i.Values1 && i.Values1.length > 0) {
          i.defaultValue = i.Values1.join('').toUpperCase()
        }
        let obj = { packageGridCode: i.gridName, packageGridValue: i.defaultValue }
        data.push(obj)
      })
      console.log(this.dataSource, '--------------')
      this.$emit('ok', data)
      this.visible=false
    },
    changeCalcParam(record) {
      let calcParam = record.calcParam
      let arr = calcParam.split(',')
      if (arr.length == 2) {
        if (Number(arr[1]) > Number(arr[0])) {
          // if (Number(arr[0]) > Number(record.orderd)
          //   || Number(arr[1]) < Number(record.orderd)) {
          //   // zhengchang
          //   record.validator = false
          // } else {
          //   record.validator = true
          // }
          record.validator = false
        } else {
          // 长度超标
          record.validator = true
        }

      } else {
        // 长度超标
        record.validator = true
      }
      console.log(record.validator)
      console.log(Number(arr[0]) >= Number(record.orderd), '0')
      console.log(Number(arr[1]) > Number(record.orderd), '1')

    },
    changeCalcCode(record) {
      record.calcParam = null
    },
    getCalcTypeList() {
      getAction(this.url.calcTypeList, { pageSize: -1 }).then(res => {
        if (res.success) {
          this.calcTypeList = res.result.records
        }
      })
    },
    customRow(record, index, event) {
      console.log(record, 're', index, event)
      return {
        props: {
          // draggable: 'true'
        },
        on: {
          // 鼠标移入
          mouseenter: (event) => {
            // 兼容IE
            var ev = event || window.event
            ev.target.draggable = true //允许拖拽
          },
          //开始拖拽
          dragstart: (event) => {
            // 兼容IE
            var ev = event || window.event
            // 阻止冒泡
            ev.stopPropagation()
            // 得到源目标数据
            this.sourceObj = record
          },
          // 拖动元素经过的元素
          dragover: (event) => {
            // 兼容 IE
            var ev = event || window.event
            // 阻止默认行为
            ev.preventDefault()
          },
          // 鼠标松开
          drop: (event) => {
            // 兼容IE
            var ev = event || window.event
            // 阻止冒泡
            ev.stopPropagation()
            // 得到目标数据
            this.targetObj = record
            let cacheData = [...this.dataSource]
            let sourceIndex = 0
            let targetIndex = 0
            let that = this
            cacheData.forEach((el, index) => {
              if (el.id == that.sourceObj.id) {
                sourceIndex = index
              }
              if (el.id == that.targetObj.id) {
                targetIndex = index
              }
            });
            //拖拽互换
            // var temp1 = cacheData[sourceIndex];
            // cacheData[sourceIndex] = cacheData[targetIndex];
            // cacheData[targetIndex] = temp1;
            //拖拽插入
            let obj = cacheData[sourceIndex]
            cacheData.splice(sourceIndex, 1);
            cacheData.splice(targetIndex, 0, obj);
            this.dataSource = [...cacheData]
          }
        },
        attrs: {
          draggable: record.draggable,
        },
      };
    },
    editChildren(record) {
      console.log(record, 'edit---')
      // this.$refs.addChildrenModal.edit(record)
    },
    StringToArr(str) {
      var list = []
      var ar2 = 0
      if (Boolean(str) && str.length > 0) {
        for (let i = 0; i < str.length - 1; i += 2) {
          list.push(str.substring(i, i + 2))
        }
        console.log(list, 'list')
        return list
      } else {
        return []
      }
    },
    handleInput(e, record) {
      record.falseFlag = false
      let val = e.target.value
      let reg = /^[A-Fa-f0-9]{2}$/
      if (!reg.test(val)) {
        record.falseFlag = true
        return false
      }
      record.Values.forEach((i, index) => {
        if (!reg.test(i)) {
          record.falseFlag = true
          return false
        }
      })
    },
    handleInput1(e, record) {
      record.falseFlag1 = false
      let val = e.target.value
      let reg = /^[A-Fa-f0-9]{2}$/
      if (!reg.test(val)) {
        record.falseFlag1 = true
        return false
      }
      record.Values1.forEach((i, index) => {
        if (!reg.test(i)) {
          record.falseFlag1 = true
          return false
        }
      })
    },
    editK(record) {
      let key = record.key
      const newData = [...this.dataSource]
      const target = newData.find((item) => key == item.key)
      this.editingKey = key
      if (target) {
        target.editable1 = true
        this.dataSource = newData
      }
    },
    save(key) {
      this.cacheData = [...this.dataSource]
      const newData = [...this.dataSource]
      const newCacheData = [...this.cacheData]
      const target = newData.find((item) => key == item.key)
      const targetCache = newCacheData.find((item) => key == item.key)
      if (target && targetCache) {
        target.packageInfoId = this.packageInfoId
        delete target.editable1
        this.dataSource = newData
        Object.assign(targetCache, target)
        this.cacheData = newCacheData
      }
      this.editingKey = ''
    },
    cancel(key) {
      const newData = [...this.dataSource]
      const target = newData.find((item) => key == item.key)
      this.editingKey = ''
      if (target) {
        Object.assign(
          target,
          this.cacheData.find((item) => key == item.key)
        )
        delete target.editable1
        this.dataSource = newData
      }
    },
    handleChange1(value, key, column, record, type) {
      if (type) record.eigenValue = ''
      if (column == 'packageGridId') {
        if (type) record.defaultValue = ''
        const select = this.packageGridIdList.find((item) => value == item.id)
        record.gridInputType = select.gridInputType
        if (select.gridInputType == 'SELECT') {
          record.eigenValueList = []
          getAction(this.url.nmcPackageGridValueList, { packageGridId: select.id }).then((res) => {
            record.eigenValueList = []
            console.log(record, 'rrecords', res.result.records, 'res.result.records')
            // record.eigenValueList = [...res.result.records]
            this.dataSource.find((i) => i.key == key).eigenValueList = [...res.result.records]
            // Vue.set(record, 'eigenValueList', [...res.result.records])
            // this.$set(record.eigenValueList, [...res.result.records])
            // this.eigenValueList = res.result.records
            console.log(record, 'sssssssssssssss')
            console.log(res.result.records, 'res.result.records')
          })
        }
        if (select.gridStaticLength) {
          record.length = select.gridStaticLength
          record.length1 = select.gridStaticLength
          record.Values = []
          record.Values1 = []
          if (record.defaultValue) {
            this.$set(record, 'Values1', [...this.StringToArr(record.defaultValue)])
          }
          if (record.eigenValue) {
            this.$set(record, 'Values', [...this.StringToArr(record.eigenValue)])
          } else {
            record.Values = []
          }
        } else {
          record.length = -1
          record.length1 = -1
        }
      }
      if (column == 'isEigen') {
        //是特征值
        let select = this.packageGridIdList.find((item) => record.packageGridId == item.id)
        record.gridInputType = select.gridInputType
        if (select && select.gridStaticLength) {
          // if (record.Values1) {
          //   this.$set(record, 'Values1', [...this.StringToArr(record.defaultValue)])
          // }

          if (value == 1) {
            if (record.eigenValue) {
              // record.Values = [...this.StringToArr(record.eigenValue)]
              this.$set(record, 'Values', [...this.StringToArr(record.eigenValue)])
            } else {
              record.Values = []
              record.length = select.gridStaticLength
              record.length1 = select.gridStaticLength
            }
          }
        } else {
          record.length = -1
          record.length1 = -1
        }
      }
      let newData = []
      this.dataSource.forEach((e) => {
        newData.push(Object.assign({}, e))
      })
      // const newData = [...this.dataSource]
      const target = newData.find((item) => key == item.key)
      if (target) {
        target[column] = value
        this.dataSource = newData
      }
    },
    onCellChange(key, dataIndex, value) {
      const dataSource = [...this.dataSource]
      const target = dataSource.find((item) => item.key == key)
      if (target) {
        target[dataIndex] = value
        this.dataSource = dataSource
      }
    },
    onDelete(record) {
      let key = record.key
      if (record.id) {
        httpAction(`${this.url.deleteP}?id=${record.id}`, {}, 'delete').then((res) => {
          this.$message.success(res.message)
        })
      }
      const dataSource = [...this.dataSource]
      this.dataSource = dataSource.filter((item) => item.key != key)
    },
    handleAdd() {
      const { count, dataSource } = this
      const newData = {
        key: count,
        packageGridId: '',
        gridInputType: '',
        packageInfoId: this.packageInfoId,
        orderd: dataSource.length + 1,
        isEigen: 0,
        eigenValue: null,
        eigenValueList: [],
        Values: [],
        falseFlag: false,
        falseFlag1: false,
        length: 1,
        editable1: true,
        calcCode: "",
        calcParam: "",
        validator: false,
      }
      this.dataSource = [...dataSource, newData]
      this.count = count + 1
      this.cacheData = [...this.dataSource]
    },
    formatName(text, type) {
      if (type == 'packageGridId') {
        let texts = this.packageGridIdList.find((i) => i.id == text)
        if (texts) {
          return texts.gridName
        } else {
          return text
        }
      }
    },
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.visible = true
      const that = this
      this.dataSource = []
      this.model = Object.assign({}, record)
      this.model.Values = []
      this.model.Values1 = []
      if (record.packageFormat == '_1553B') {
        this.is1553B = true
      } else {
        this.is1553B = false
      }
      //工程量全部列表
      getAction(`${this.url.PackageGridlist}`, { pageSize: -1 }).then((res) => {
        //该包的工程量列表
        this.packageInfoId = record.id
        this.packageGridIdList = res.result.records
        getAction(`${this.url.query}?packageInfoId=${record.id}`, {
          pageSize: -1,
          order: 'asc',
          column: 'orderd',
        }).then((res) => {
          if (res.result && res.result.records && res.result.records.length != 0) {
            // console.log(res.result.records, 'res.result.records')
            res.result.records.forEach((i, index) => {
              i.key = index
              if (i.gridInputType == 'SELECT') {
                // console.log(i, 'iiiiiiiiii')
                that.handleChange1(i.packageGridId, i.key, 'packageGridId', i)
              }
              this.dataSource.push(i)
            })
          } else {
            this.dataSource = []
            // this.dataSource = [...this.model.dataSource]
          }
          this.count = this.dataSource.length
          this.dataSource.forEach((i) => {
            // let select = that.packageGridIdList.find((item) => i.packageGridId == item.id)

            if (i.length) {
              this.$set(i, 'Values1', [...this.StringToArr(i.defaultValue)].slice(0, i.length))
              i.length1 = i.length
              if (i.isEigen == 1) {
                i.length = i.length
                // i.length = i.gridStaticLength
                this.$set(i, 'Values', [...this.StringToArr(i.eigenValue)])
              }
            } else {
              i.length = -1
              i.length1 = -1
            }
            this.dataSource = [...this.dataSource]
          })
        })
      })
      // //工程量全部列表
      // getAction(`${this.url.PackageGridlist}`, { pageSize: -1 }).then((res) => {
      //   //该包的工程量列表
      //   this.packageInfoId = record.id
      //   this.packageGridIdList = res.result.records
      // })
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          // that.confirmLoading = true
          let httpurl = ''
          let method = ''
          let flag = false
          let flag1 = false
          let eigen = false //保证至少一个是特征值
          let validatorFlag = true
          let eigenAndDefaultValue = true //保证是特征值的特征值与默认值相等
          this.dataSource.forEach((i, index) => {
            i.orderd = index + 1  //组包排序赋值
            if (i.validator) {
              validatorFlag = false
            }
            if (i.gridInputType == 'INPUT' && i.Values1 && i.Values1.length > 0) {
              i.defaultValue = i.Values1.join('').toUpperCase()
            }
            if (i.isEigen && i.Values) {
              if (i.Values.length > 0) {
                i.eigenValue = i.Values.join('').toUpperCase()
              }
            }
            if (i.isEigen == 1) {
              eigen = true//
              if (i.eigenValue != i.defaultValue) {
                eigenAndDefaultValue = false
              }
            }
            if (i.falseFlag == true) {
              flag = true
            }
            if (i.falseFlag1 == true) {
              flag1 = true
            }
          })
          console.log(this.is1553B, 'this.is1553B')
          if (!this.is1553B) {
            if (!eigen) {
              this.$message.error('至少存在一个是特征值！！！')
              return
            }
          }

          if (!eigenAndDefaultValue) {
            this.$message.error('是特征值的工程量必须保证特征值与默认值一样！！！')
            return
          }
          if (!validatorFlag) {
            this.$message.error('计算参数出错～')
            return
          }

          this.model.dataSource = [...this.dataSource]
          if (this.is1553B) {
            if (!flag && !flag1 && validatorFlag) {
              // console.log(this.dataSource, 'this.dataSource提交数据')
              that.confirmLoading = true
              httpAction(this.url.addP, this.dataSource, 'post')
                .then((res) => {
                  if (res.success) {
                    that.$message.success(res.message)
                    that.$emit('ok')
                  } else {
                    that.$message.warning(res.message)
                  }
                })
                .finally(() => {
                  that.confirmLoading = false
                })
            }
          } else {
            if (!flag && !flag1 && eigenAndDefaultValue && eigen && validatorFlag) {
              // console.log(this.dataSource, 'this.dataSource提交数据')
              // return

              that.confirmLoading = true


              httpAction(this.url.addP, this.dataSource, 'post')
                .then((res) => {
                  if (res.success) {
                    that.$message.success(res.message)
                    that.$emit('ok')
                  } else {
                    that.$message.warning(res.message)
                  }
                })
                .finally(() => {
                  that.confirmLoading = false
                })
            }
          }
          // if (!flag && !flag1 && eigenAndDefaultValue && eigen && validatorFlag) {
          //   // console.log(this.dataSource, 'this.dataSource提交数据')
          //   // return

          //   that.confirmLoading = true


          //   httpAction(this.url.addP, this.dataSource, 'post')
          //     .then((res) => {
          //       if (res.success) {
          //         that.$message.success(res.message)
          //         that.$emit('ok')
          //       } else {
          //         that.$message.warning(res.message)
          //       }
          //     })
          //     .finally(() => {
          //       that.confirmLoading = false
          //     })
          // }
          return
          this.model.dataSource.forEach((item) => {
            let httpurl = ''
            if (!item.id) {
              httpurl += that.url.addP
              method = 'post'
            } else {
              httpurl += that.url.editP
              method = 'put'
            }
            delete item.key
            httpAction(httpurl, item.id ? item : [item], method)
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message)
                  that.$emit('ok')
                } else {
                  that.$message.warning(res.message)
                }
              })
              .finally(() => {
                that.confirmLoading = false
              })
          })
        }
      })
    },
  },
}
</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}
</style>
